const Utils = require('./Utils.js')
Utils.init()
var flag, one_flag, count, num, region, seconds

count = 0
num = 0
seconds = 0
region = [375, 160, 895, 370]
flag = true
one_flag = true

while (flag) {
    Utils.sleeps()
    if (Utils.findPictureTap('勋章实', region) || Utils.findPictureTap('勋章空', region) || Utils.findPictureTap('勋章空备用', region) || Utils.findPictureTap('勋章实备用', region)) {
        log('点击结界 - 成功') 
        while (one_flag) {
            if (Utils.findPictureTap('奖励')) {
                count += 1
                one_flag = false
                /********** 突破刷新点击领取奖励 start **********/
                for (let i = 0; i < 3; i++){
                    Utils.sleeps()
                    if (Utils.findPictureTap('奖励')) {
                        break
                    }
                }
                /********** 突破刷新点击领取奖励 end **********/
            } else if (Utils.findPictureTap('失败')) {
                one_flag = false
            } else {
                if (Utils.findPictureTap('进攻', 0.8)) {
                    Utils.waitTime(10)
                    if (Utils.findPicture('进攻', 0.8)) {
                        log('结界挑战券不足')
                        Utils.stopJB()
                    }
                    Utils.findPictureTap('准备')
                    sleep(1000)
                    log('进攻 - 成功')
                    seconds = random(20, 30)
                    Utils.waitTime(seconds)
                }
                log('已等待：' + (seconds+=2) + '秒')
            }
        }
        one_flag = true
        num = 0
        seconds = 0
    } else if (num === 5) {
        let flagTemp = true
        while (flagTemp) {
            if (Utils.findPictureTap('刷新', 0.95)) {
                log('刷新 - 成功')
            } else if (Utils.findPictureTap('确定')) {
                log('点击确定 - 成功')
                flagTemp = false
            }
        }
        num = 0
    } else {
        num++
        log('检测待突破结界第 '+ num +' 次')
    }
    log('总共突破结界数：'+ count)
}
